package com.wedo.mall.activity.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.widget.ETitleBar;
import com.wedo.mall.R;
import com.wedo.mall.WApp;
import com.wedo.mall.entity.Consignee;
import com.wedo.mall.entity.GoodsInfo;
import com.wedo.mall.entity.PointOrderDetail;
import com.wedo.mall.util.WUrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

public class ExchangeActivity extends EActivity {

    public static final String EXTRA_GOODS_ID = "goods_id";
    public static final String EXTRA_ORDER_SN = "order_sn";

//    public static final int EXPRESS_DOORTODOOR = 0;
//    public static final int EXPRESS_TOSTORE = 1;
//
//    private int express_type = EXPRESS_DOORTODOOR;

    private String goods_id;
    private String order_sn;
//    private String consignee_id = "";
//    @BindView(R.id.ll_deliverstyles) LinearLayout ll_deliverstyles;
//    @BindView(R.id.tv_consignee) TextView tv_consignee;
//    @BindView(R.id.tv_telephone) TextView tv_telephone;
//    @BindView(R.id.tv_address) TextView tv_address;
    @BindView(R.id.tv_agent_name) TextView tv_agent_name;
    @BindView(R.id.iv_goods_image) ImageView iv_goods_image;
    @BindView(R.id.tv_goods_name) TextView tv_goods_name;
    @BindView(R.id.tv_point) TextView tv_point;
//    @BindView(R.id.tv_order_sn) TextView tv_order_sn;
//    @BindView(R.id.label_doortodoor) TextView label_doortodoor;
//    @BindView(R.id.label_tostore) TextView label_tostore;
//    @BindView(R.id.iv_down_arrow) ImageView iv_down_arrow;
    @BindView(R.id.btn_buy_now) Button btn_buy_now;
//    @BindView(R.id.rl_deliverstyle) RelativeLayout rl_deliverstyle;
    @BindView(R.id.title_bar) ETitleBar title_bar;


//    @BindViews({R.id.iv_spot_doortodoor,R.id.iv_spot_tostore}) List<ImageView> ivs;


    public static void toDetail(EActivity activity,String goods_id,String type){
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_GOODS_ID,goods_id);
        bundle.putString("ACTIVITY_TYPE",type);
        activity.toActivity(ExchangeActivity.class,bundle);
    }

    public static void toExchangeDetail(Context context, String order_sn,String type){
/*        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_ORDER_SN,order_sn);*/
        Intent intent = new Intent(context,ExchangeActivity.class);
        intent.putExtra(EXTRA_ORDER_SN,order_sn);
        intent.putExtra("ACTIVITY_TYPE",type);
        context.startActivity(intent);
    }

    @Override
    protected int layout() {
        return R.layout.activity_exchange;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        if(getIntent().getStringExtra("ACTIVITY_TYPE").equals("0")){
            goods_id = getIntent().getStringExtra(EXTRA_GOODS_ID);
            getPointInfo();
//            tv_order_sn.setText(label_doortodoor.getText().toString());
        }else if(getIntent().getStringExtra("ACTIVITY_TYPE").equals("1")){
            btn_buy_now.setVisibility(View.GONE);
//            rl_deliverstyle.setOnClickListener(null);
//            iv_down_arrow.setVisibility(View.GONE);
            title_bar.setTitle(R.string.user_exchangedetail);

            order_sn = getIntent().getStringExtra(EXTRA_ORDER_SN);
            getPointOrderDetail();
        }

    }

    /**
     * 获取积分商品兑换页接口
     */
    private void getPointInfo(){
        Map<String,String> params = new HashMap<>((1));
        params.put(EXTRA_GOODS_ID,goods_id);
        loading();
        post(EApi.USER_POINTINFO, params, new EObserver() {
            @Override
            public void successful(EResponse response) {

//                Consignee consignee = response.object("consignee",Consignee.class);
//                if(consignee != null) {
//
//                    tv_consignee.setText(consignee.consignee);
//                    tv_telephone.setText(consignee.telephone);
//                    if(consignee.city_name.equals(consignee.county_name)){
//                        tv_address.setText(consignee.county_name + consignee.address);
//                    }else if(consignee.province_name.equals(consignee.city_name)){
//                        tv_address.setText(consignee.city_name + consignee.county_name + consignee.address);
//                    }else{
//                        tv_address.setText(consignee.province_name + consignee.city_name + consignee.county_name + consignee.address);
//                    }
//                    consignee_id = consignee.id;
//
//                }else {
//                    toast(R.string.address_error);
//                }

                GoodsInfo goodsInfo = response.object("goodsInfo",GoodsInfo.class);
                if(goodsInfo.agent_name != null){
                    tv_agent_name.setText(goodsInfo.agent_name);
                }
                tv_goods_name.setText(goodsInfo.goods_name);
                tv_point.setText(goodsInfo.goods_price);
                Glide.with(ExchangeActivity.this).load(WUrl.url(goodsInfo.goods_image)).error(R.drawable.place_holder_agent).placeholder(R.drawable.place_holder_agent).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_goods_image);
                dismiss();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    /**
     * 获取订单接口
     */
    private void getPointOrderDetail(){
        Map<String,String> params =  new HashMap<>((1));
        params.put(EXTRA_ORDER_SN,order_sn);
        loading();
        post(EApi.USER_POINTORDERDETAIL, params, new EObserver() {
            @Override
            public void successful(EResponse response) {
                PointOrderDetail pod = response.object("list",PointOrderDetail.class);
//                tv_consignee.setText(pod.getConsignee());
//                tv_telephone.setText(pod.getTelephone());
//                if(pod.getCity().equals(pod.getCounty())){
//                    tv_address.setText(pod.getCounty() + pod.getAddress());
//                }else if(pod.getProvince().equals(pod.getCity())){
//                    tv_address.setText(pod.getCity() + pod.getCounty() + pod.getAddress());
//                }else{
//                    tv_address.setText(pod.getProvince() + pod.getCity() + pod.getCounty() + pod.getAddress());
//                }
                tv_agent_name.setText(pod.getAgent_name());

//                if(pod.getExpress_type().equals("0")){
//                    tv_order_sn.setText(label_doortodoor.getText().toString());
//                }else if(pod.getExpress_type().equals("1")){
//                    tv_order_sn.setText(label_tostore.getText().toString());
//                }

                PointOrderDetail.GoodsListBean goodsInfo = pod.getGoodsList().get(0);
                tv_goods_name.setText(goodsInfo.getGoods_name());
                tv_point.setText(goodsInfo.getPrice());
                Glide.with(ExchangeActivity.this).load(WUrl.url(goodsInfo.getGoods_image())).error(R.drawable.place_holder_agent).placeholder(R.drawable.place_holder_agent).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_goods_image);
                dismiss();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
                defaultFinish();
            }
        });
    }

    private void commitPointInfo(){
        Map<String,String> params = new HashMap<>();
        params.put(EXTRA_GOODS_ID,goods_id);
        params.put("express_type","1");
//        if(express_type == EXPRESS_DOORTODOOR){
//            params.put("consignee_id",consignee_id);
//        }
        loading();
        post(EApi.USER_COMMITPOINTINFO, params, new EObserver() {
            @Override
            public void successful(EResponse response) {
                String order_sn = response.string("order_sn");
                ExchangeSuccessActivity.toDetail(ExchangeActivity.this,order_sn);
                toast(getString(R.string.success));
                dismiss();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }


    @OnClick(R.id.btn_buy_now)
    protected void onClick(){
        commitPointInfo();
    }

//    @OnClick(R.id.rl_deliverstyle)
//    protected void onDeliverStyleClick(){
//        if(ll_deliverstyles.getVisibility() == View.GONE){
//            ll_deliverstyles.setVisibility(View.VISIBLE);
//            iv_down_arrow.setRotation(180);
//        }else if(ll_deliverstyles.getVisibility() == View.VISIBLE){
//            ll_deliverstyles.setVisibility(View.GONE);
//            iv_down_arrow.setRotation(0);
//        }
//    }

//    @OnClick({R.id.rl_doortodoor,R.id.rl_tostore})
//    protected void onExpressClick(View view){
//        switch (view.getId()){
//            case R.id.rl_doortodoor:
//                express_type = EXPRESS_DOORTODOOR;
//                break;
//            case R.id.rl_tostore:
//                express_type = EXPRESS_TOSTORE;
//                break;
//        }
//
//        onExpressChanged();
//    }

//    private void onExpressChanged(){
//        for (ImageView v:ivs){
//            Glide.with(ExchangeActivity.this).load(R.drawable.spot_unselected).diskCacheStrategy(DiskCacheStrategy.ALL).into(v);
//        }
//        Glide.with(ExchangeActivity.this).load(R.drawable.spot_selected).diskCacheStrategy(DiskCacheStrategy.ALL).into(ivs.get(express_type));
//
//        switch (express_type){
//            case EXPRESS_DOORTODOOR:
//                tv_order_sn.setText(label_doortodoor.getText().toString());
//                break;
//            case EXPRESS_TOSTORE:
//                tv_order_sn.setText(label_tostore.getText().toString());
//                break;
//        }
//
//        ll_deliverstyles.setVisibility(View.GONE);
//        iv_down_arrow.setRotation(0);
//    }
}
